﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WbsEditor
{
    public partial class IterationDashboardForm : Form
    {
        public IterationDashboardForm()
        {
            InitializeComponent();
        }

        public IterationDashboard DashboardControl
        {
            get
            {
                return _iterationDashboard;
            }
        }

        private void IterationDashboardForm_Load(object sender, EventArgs e)
        {
            if (DashboardControl.TfsClient.IsConnected)
            {
                _iterationCombo.Items.Clear();
                string[] iterations = DashboardControl.TfsClient.GetTeamProjectIterations();
                _iterationCombo.Items.AddRange(iterations);
                _iterationCombo.Text = DashboardControl.TfsClient.Iteration;
            }
        }

        private void _iterationCombo_TextChanged(object sender, EventArgs e)
        {
            if (DashboardControl.TfsClient.IsConnected)
            {
                DashboardControl.TfsClient.Iteration = _iterationCombo.Text;
            }
        }

        private void _addIterationButton_Click(object sender, EventArgs e)
        {
            DashboardControl.AddIterationToDisplay(_iterationCombo.Text);
        }

        private void _closeButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void IterationDashboardForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DashboardControl.HasChanges())
            {
                DialogResult result = MessageBox.Show("There have been changes to the iterations.\n\nDo you want to save them now?",
                    "Confirm", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (DialogResult.Cancel == result)
                {
                    e.Cancel = true;
                }
                else if (DialogResult.Yes == result)
                {
                    DashboardControl.CommitChanges();
                }
            }
        }
    }
}
